//var mqtt = require('utils/mqtt.min');
//app.js
App({
  onLaunch: function() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.globalData.sysinfo = wx.getSystemInfoSync()
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserProfile({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    // 获取系统状态栏信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
         	this.globalData.Custom = capsule;
        	this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
        	this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
  },
  globalData: {
    userInfo: null
  },

  getModel: function () { //获取手机型号
    return this.globalData.sysinfo["model"]
  },
  getVersion: function () { //获取微信版本号
    return this.globalData.sysinfo["version"]
  },
  getSystem: function () { //获取操作系统版本
    return this.globalData.sysinfo["system"]
  },
  getPlatform: function () { //获取客户端平台
    return this.globalData.sysinfo["platform"]
  },
  getSDKVersion: function () { //获取客户端基础库版本
    return this.globalData.sysinfo["SDKVersion"]
  },
  buf2string: function (buffer) {
    var arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
    var str = ''
    for (var i = 0; i<arr.length; i++) {
    str += String.fromCharCode(arr[i])
  }
  return str
  },
  buf2hex: function (buffer) {
    return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
  },
  string2buf: function (str) {
    // 首先将字符串转为16进制
    let val = ""
    for (let i = 0; i < str.length; i++) {
      if (val === '') {
        val = str.charCodeAt(i).toString(16)
      } else {
        val += ',' + str.charCodeAt(i).toString(16)
      }
    }
    // 将16进制转化为ArrayBuffer
    return new Uint8Array(val.match(/[\da-f]{2}/gi).map(function (h) {
      return parseInt(h, 16)
    })).buffer
  },



    /**
   * 字符串每20个字节分包返回数组
   */
  subPackage: function (str) {
    const packageArray = []
    for (let i = 0; str.length > i; i += 20) {
      packageArray.push(str.substr(i, 20))
    }
    return packageArray
  },
  /**
   * 停止搜索附近蓝牙
   */
  stopSearchDevs: function () {
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) { },
    })
  },
  /**
   * 开始连接
   */
  startConnect(deviceId, deviceName) {
    if (this.globalData.BluetoothState) {
      wx.createBLEConnection({
        deviceId: deviceId,
        timeout: 10000, // 10s连接超时
        success: function (res) {
          wx.navigateTo({
            url: `/pages/service/service?deviceId=${deviceId}&deviceName=${deviceName}`,
          })
        },
      })
    }
  },
  /**
   * 断开连接
   */
  endConnect(deviceId) {
    if (this.globalData.BluetoothState) {
      wx.closeBLEConnection({
        deviceId: deviceId,
        success: function (res) {},
      })
    }
  },
    //buff分包,20个一包
    getSendPackage:function(buff){
      const packageArray = []
      for(let i = 0; i<buff.byteLength;i+=20){
        if(i<buff.byteLength){
          packageArray.push(buff.slice(i,i+20))
        }else{
          packageArray.push(i,buff.byteLength)
          break
        }
      }
      return packageArray
    }






})